//
//  MainViewController.m
//  LvBao
//
//  Created by 范琦 on 16/6/2.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "MainViewController.h"
#import "QRCodeReaderViewController.h"
#import "QRCodeReader.h"
#import "FLAlertViewController.h"
#import "MainTopView.h"
#import "MainTableViewCell.h"
#import "OrderDetailViewController.h"
#import "ScanCodeViewController.h"
#import "ExchangeViewController.h"
#import "CashierViewController.h"

@interface MainViewController ()<QRCodeReaderDelegate,UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) MainTopView *topView;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MainViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.navigationBar.translucent = NO;
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    button.frame = CGRectMake(100, 100, 100, 100);
//    button.backgroundColor = [UIColor redColor];
//    [self.view addSubview:button];
//    [button addTarget:self action:@selector(saomiao) forControlEvents:UIControlEventTouchUpInside];
    
    [self creatTopView];
    
    [self creatTableView];
}

- (void)creatTopView
{
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"MainTopView" owner:self options:nil] lastObject];
    
    _topView.frame = CGRectMake(0, 20, KSCREENWIDTH, KSCREENHEIGHT / 3.2);
    [_topView.scanCode addTarget:self action:@selector(scanCode) forControlEvents:UIControlEventTouchUpInside];
    [_topView.exchange addTarget:self action:@selector(exchange) forControlEvents:UIControlEventTouchUpInside];
    [_topView.cashierB addTarget:self action:@selector(cashier) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_topView];

}

- (void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _topView.frame.size.height + 20 - 64, KSCREENWIDTH, KSCREENHEIGHT  - _topView.bounds.size.height) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"MainTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    _tableView.rowHeight = 115;
    _tableView.sectionFooterHeight = 0;
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 30)];
    label.text = @"  最近订单记录";
    label.font = [UIFont systemFontOfSize:15];
    label.backgroundColor = [UIColor huiseColor];
    _tableView.tableHeaderView = label;

}

// 扫码
- (void)scanCode
{
//    //二维码
//    if ([QRCodeReader supportsMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]) {
//        static QRCodeReaderViewController *vc = nil;
//        static dispatch_once_t onceToken;
//        
//        dispatch_once(&onceToken, ^{
//            QRCodeReader *reader = [QRCodeReader readerWithMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
//            vc                   = [QRCodeReaderViewController readerWithCancelButtonTitle:@"Cancel" codeReader:reader startScanningAtLoad:YES showSwitchCameraButton:YES showTorchButton:YES];
//            vc.modalPresentationStyle = UIModalPresentationFormSheet;
//        });
//        vc.delegate = self;
//        
//        [vc setCompletionWithBlock:^(NSString *resultAsString) {
//            NSLog(@"Completion with result: %@", resultAsString);
//        }];
//        
//        [self presentViewController:vc animated:YES completion:NULL];
//    }
//    else {
//        [FLAlertViewController alertControllerWithTitle:@"Error" message:@"Reader not supported by the current device" preferredStyle:UIAlertControllerStyleActionSheet actionArr:nil dismissTimer:1];
//        
//    }
 
    ScanCodeViewController *scan = [[ScanCodeViewController alloc]init];
    [self.navigationController pushViewController:scan animated:YES];
    
}
- (void)exchange
{
    ExchangeViewController *scan = [[ExchangeViewController alloc]init];
    [self.navigationController pushViewController:scan animated:YES];
}

- (void)cashier
{
    CashierViewController *cashier = [[CashierViewController alloc]init];
    [self.navigationController pushViewController:cashier animated:YES];

}

#pragma mark - QRCodeReader Delegate Methods
- (void)reader:(QRCodeReaderViewController *)reader didScanResult:(NSString *)result
{
    [self dismissViewControllerAnimated:YES completion:^{
        [FLAlertViewController alertControllerWithTitle:@"QRCodeReader" message:result preferredStyle:UIAlertControllerStyleActionSheet actionArr:nil dismissTimer:1];
    }];
    
    
}

- (void)readerDidCancel:(QRCodeReaderViewController *)reader
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

#pragma mark - TableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    return 10;
}



// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    OrderDetailViewController *order = [[OrderDetailViewController alloc]init];
    [self.navigationController pushViewController:order animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
